package com.hnkywl.sts.service.taskdeal;

import com.hnkywl.sts.dao.cggl.BgcgdSyncDao;
import com.hnkywl.sts.dao.cggl.CgdDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-8-19
 * Time: 下午6:49
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DoBgcgdSync {
    private BgcgdSyncDao bgcgdSyncDao;
    private CgdDao cgdDao;
    //手动执行方法
    public void doBgcgdSync(){
        List<Map> bgcgd = findBgcgd();
        if(!CollectionUtils.isEmpty(bgcgd)){
            for(Map item : bgcgd){
                doBgcgdSync(item);
            }
        }
    }

    @Transactional(readOnly=true)
    public List<Map> findBgcgd(){
        return cgdDao.findBgcgdSyncList();
    }

    @Transactional
    public void doBgcgdSync(Map item){
        try{
            switch (((BigDecimal)item.get("ZT")).intValue()){
                case 1 :            //新增保供采购单
                    bgcgdSyncNew(((BigDecimal)item.get("CGD_ID")).longValue());
                    break;
                case 2 :            //入库
                    bgcgdSyncRk(((BigDecimal)item.get("CGD_ID")).longValue());
                    break;
                case 3 :            //审核
                    bgcgdSyncTz(((BigDecimal)item.get("CGD_ID")).longValue());
                    break;
                case 4:             //删除
                    bgcgdSyncDel(((BigDecimal)item.get("CGD_ID")).longValue());
                    break;
                case 5:             //结算
                    bgcgdSyncJs(((BigDecimal)item.get("CGD_ID")).longValue());
                    break;
                case 6:             //删除结算
                    bgcgdSyncDelJs(((BigDecimal)item.get("CGD_ID")).longValue());
            }
            String sql = "update t_biz_cgd_wg set flag = 2, last_updated = ? where id = ?";
            cgdDao.executeSQL(sql,new Date(),item.get("ID"));
        } catch (Exception e){
            e.printStackTrace();
            String sql = "update t_biz_cgd_wg set flag = 3, last_updated = ?,except = ? where id = ?";
            cgdDao.executeSQL(sql,new Date(),e.getMessage(),item.get("ID"));
        }

    }

    public void bgcgdSyncNew(Long cgdId){
        //插入主表信息
        Object[] cgd = cgdDao.findBgcgdSyncDetail(cgdId);
        //明细信息
        List<Object[]> cgdmx = cgdDao.findBgcgdSyncMxList(cgdId);

        bgcgdSyncDao.bgcgdSyncNew(cgd,cgdmx);
    }
    public void bgcgdSyncRk(Long cgdId){
        //主信息表更新
        Object[] dhxx = cgdDao.findBgcgdSyncDhxx(cgdId);
        //明细表更新
        List<Object[]> dhmx = cgdDao.findBgcgdSyncDhmx(cgdId);
        //未结算物资(wjswz)主表信息
        Object[] wjswz = cgdDao.findWjswz(cgdId);
        //未结算物资明细(wjswzmx)明细表信息
        List<Object[]> wjswzmx = cgdDao.findWjswzmx(cgdId);
        bgcgdSyncDao.bgcgdSyncRk(dhxx,dhmx,wjswz,wjswzmx,cgdId+200000);
    }
    public void bgcgdSyncTz(Long cgdId){
        bgcgdSyncDao.bgcgdSyncTz(cgdId+200000);
    }

    public void bgcgdSyncDel(Long cgdId){
        String mxIds = cgdDao.findBgcgdSyncMxIds(cgdId);
        bgcgdSyncDao.bgcgdSyncDel(cgdId,mxIds);
    }

    public void bgcgdSyncJs(Long cgdId){
        bgcgdSyncDao.bgcgdSyncJs(cgdId+200000);
    }
    public void bgcgdSyncDelJs(Long cgdId){
        bgcgdSyncDao.bgcgdSyncDelJs(cgdId+200000);
    }

    @Autowired
    public void setBgcgdSyncDao(BgcgdSyncDao bgcgdSyncDao) {
        this.bgcgdSyncDao = bgcgdSyncDao;
    }
    @Autowired
    public void setCgdDao(CgdDao cgdDao) {
        this.cgdDao = cgdDao;
    }
}
